package com.test.suanfa;

import java.util.Arrays;

/**
 * 给定数组{1,7,3,6,5,6}中找出中心下标
 * note:左边的数=右边的数
 */
public class 中心下标 {

    static int[] nums = {1, 7, 3, 6, 5, 6};

    public static void main(String[] args) {
        test1();

    }

    public static void test2() {
        int sum = Arrays.stream(nums).sum();
        int total = 0;
        for (int i=0;i<nums.length;i++){
            total = total + nums[i];
            if (total == sum){
                System.out.println(i);
                break;
            }
            sum = sum - nums[i];
        }
    }

    public static void test1() {
        int sum = Arrays.stream(nums).sum();
        int total = 0;
        for (int i = nums.length - 1; i > 0; i--) {
            total = total + nums[i];
            if (total == sum) {
                System.out.println(i);
                break;
            }
            sum = sum - nums[i];
        }
    }
}
